package com.bookmarket.controller;

import com.bookmarket.entity.Book;
import com.bookmarket.entity.Btype;
import com.bookmarket.entity.Stype;
import com.bookmarket.service.LLBookService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BookController
 *
 * @author 李亮
 * @date 2017/12/26
 */
@RestController
@RequestMapping("/book")
public class LLBookController {
    @Resource
    private LLBookService bookService;

    @RequestMapping("/getpage")
    public Map queryUsersByPage(int pageNum, int pageSize, String name) {
        if (name == null) {
            name = "";
        }

        Map map = new HashMap();
        ModelAndView mv = new ModelAndView();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("row", (pageNum - 1) * pageSize);
        map.put("name", name);
        Map map1 = bookService.getall(map);
        return map1;
    }


    @RequestMapping("/update")
    public ModelAndView update(int id,String name,String s) {
        ModelAndView mv = new ModelAndView();
        Book book=bookService.getbook(id);
        System.out.println(book.getStype().getId());
        if(name.equals("name")){
            book.setName(s);
        }
        if(name.equals("author")){
            book.setAuthor(s);
        }
        if(name.equals("publish")){
            book.setPublish(s);
        }
        if(name.equals("publish_date")){
            book.setPublish_date(s);
        }
        if(name.equals("stock")){
            book.setStock(Integer.parseInt(s));
        }
        if(name.equals("price")){
            book.setPrice(new Float(s));
        }
        if(name.equals("click")){
            book.setClick(Integer.parseInt(s));
        }
        if(name.equals("content")){
            book.setContent(s);
        }
        if(name.equals("stype")){
            Stype stype=new Stype(Integer.parseInt(s));
            book.setStype(stype);
            bookService.updat1(book);
        }
        bookService.update(book);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/dell")
    public ModelAndView dell(int id) {
        ModelAndView mv = new ModelAndView();
        bookService.delet(id);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
  @RequestMapping("/getbtype")
    public ModelAndView toBook(){

      ModelAndView mv = new ModelAndView();
    List<Btype> btypelist=bookService.getallbtype();
      mv.setView(new MappingJackson2JsonView());
      mv.addObject("btypelist", btypelist);
            return   mv;
    }
    @RequestMapping("/getstype")
    public ModelAndView getstype(int id){
        ModelAndView mv = new ModelAndView();
        List<Stype> list=bookService.getstype(id);
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("stypelist",list);
        return   mv;
    }

    public LLBookService getBookService() {
        return bookService;
    }

    public void setBookService(LLBookService bookService) {
        this.bookService = bookService;
    }
}